package com.example.homework5;



import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;



public class MyFragment2 extends Fragment {
    private Button mBtnOpenBaiDu;
    private Button mBtnOpenBili;
    private Button mBtnOpenEasyMusic;

    public MyFragment2() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_change2, container, false);

        initView(view);
        initEvent(view);

        return view;
    }

    private void initView(View view) {
        mBtnOpenBaiDu = view.findViewById(R.id.btn_open_BaiDu);
        mBtnOpenBili = view.findViewById(R.id.btn_open_BiliBili);
        mBtnOpenEasyMusic = view.findViewById(R.id.btn_open_easy_music);
    }


    private void initEvent(View view) {
        mBtnOpenBaiDu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(view.getContext(), "打开百度", Toast.LENGTH_SHORT).show();
                //跳转到百度
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                intent.setData(Uri.parse("https://m.baidu.com/"));
                startActivity(intent);
            }
        });

        mBtnOpenBili.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(view.getContext(), "打开bili", Toast.LENGTH_SHORT).show();
                try {
                    //跳转到b站
                    Intent intent = view.getContext().getPackageManager().getLaunchIntentForPackage(Config.PACKAGE_NAME_BILIBILI_TV);
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(view.getContext(), "还未安装bilibili客户端，请先下载app", Toast.LENGTH_SHORT).show();
                }
            }
        });

        mBtnOpenEasyMusic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(view.getContext(), "打开网易云音乐", Toast.LENGTH_SHORT).show();
                try {
                    //跳转到网易云
                    Intent intent =  view.getContext().getPackageManager().getLaunchIntentForPackage(Config.PACKAGE_NAME_NETEASE_CLOUDMUSIC);
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText( view.getContext(), "还未安装网易云音乐客户端，请先下载app", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
